package com.prs.crm.action.person;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.person.Contract;
import com.prs.crm.service.person.ContractService;

public class ContractAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private Integer[] ids;
	private PaginationSupport pagination;
	private Contract contract;
	private ContractService contractService;
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Contract getContract() {
		return contract;
	}
	public void setContract(Contract contract) {
		this.contract = contract;
	}
	public ContractService getContractService() {
		return contractService;
	}
	public void setContractService(ContractService contractService) {
		this.contractService = contractService;
	}
	@Action("listContract")
	public String listContract(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(Contract.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getContractService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action("editContract")
	public String editContract(){
		if(ids!=null){
			this.contract=this.getContractService().get(ids[0]);
		}
		return SUCCESS;
	}
	@Action(value="saveContract",results={
			@Result(name="success",type="redirect",location="listContract"),
			@Result(name="error",type="chain",location="editContract")
	})
	public String saveContract(){
		this.getContractService().save(contract);
		return SUCCESS;
	}
	
	@Action(value="removeContract",results={
			@Result(name="success",type="redirect",location="listContract"),
			@Result(name="error",type="chain",location="listContract")
	})
	public String removeContract(){
		if (ids==null) {
			this.addActionError("请选择一行进行操作!");
			return ERROR;
		}
		this.getContractService().batchRemove(ids);
		return SUCCESS;
	}
}
